<html>
<head><meta charset="utf-8"><title>Intrinsic methods with trait constraints · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic.20methods.20with.20trait.20constraints.html">Intrinsic methods with trait constraints</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="179721517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic%20methods%20with%20trait%20constraints/near/179721517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Zimmer <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic.20methods.20with.20trait.20constraints.html#179721517">(Nov 02 2019 at 22:22)</a>:</h4>
<p>I have never worked on rust analyser before, but I ran into this issue <a href="https://github.com/rust-analyzer/rust-analyzer/issues/2138" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/2138">https://github.com/rust-analyzer/rust-analyzer/issues/2138</a> on a personal project so I figured I'd try to fix it myself. It seems to be pretty easy to fix the precise sample in that issue, but it looks harder to fix the problem I was having myself.</p>
<div class="codehilite"><pre><span></span>           trait Foo {
                fn foo(&amp;self);
            }
            trait Bar {
                fn bar(&amp;self);
            }
            struct X();
            impl Foo for X {
                fn foo(&amp;self) {}
            }

            struct Wrapper&lt;T&gt;(T);
            impl&lt;T: Foo&gt; Wrapper&lt;T&gt; {
                fn wrapper_foo(&amp;self) { self.0.foo() }
            }
            impl&lt;T: Bar&gt; Wrapper&lt;T&gt; {
                fn wrapper_bar(&amp;self) { self.0.bar() }
            }
            fn main() {
                let wrapper = Wrapper(X());
                let x = wrapper.&lt;|&gt;
            }
</pre></div>


<p>(The completions should contain <code>wrapper_foo</code> but not <code>wrapper_bar</code>). The way that inherent methods work atm seems weird to me, wouldn't it make more sense if each inherent impl block created a trait? That way chalk could do the work of determining which inherent impl blocks apply at a given call site.</p>



<a name="179721892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic%20methods%20with%20trait%20constraints/near/179721892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic.20methods.20with.20trait.20constraints.html#179721892">(Nov 02 2019 at 22:35)</a>:</h4>
<p>That might work to basically avoid having to do the unification and checking the where clauses ourselves, but note that inherent methods behave somewhat differently from traits, e.g. they don't need to be in scope and are chosen with priority over traits, so they need special handling anyway; and doing the unification properly isn't that hard either, so I don't feel like it would be worth it</p>



<a name="179721939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic%20methods%20with%20trait%20constraints/near/179721939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic.20methods.20with.20trait.20constraints.html#179721939">(Nov 02 2019 at 22:36)</a>:</h4>
<p>also note that I'm currently working on refactoring method resolution as well to get the priority order really correct</p>



<a name="179760836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic%20methods%20with%20trait%20constraints/near/179760836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Zimmer <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic.20methods.20with.20trait.20constraints.html#179760836">(Nov 03 2019 at 19:54)</a>:</h4>
<p>I've got a mostly working implementation of this now, but it breaks the test <code>obligation_from_impl_clause</code>. I don't understand how this test is supposed to work though, I get error E0207 when I try to compile it with rustc.</p>



<a name="179761560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic%20methods%20with%20trait%20constraints/near/179761560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Intrinsic.20methods.20with.20trait.20constraints.html#179761560">(Nov 03 2019 at 20:12)</a>:</h4>
<p>here's a slightly more complicated version that compiles in rustc: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=351a23ed72c6de64f600cc7c190a6faf" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=351a23ed72c6de64f600cc7c190a6faf">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=351a23ed72c6de64f600cc7c190a6faf</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>